package com.xm.servlet.admin;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.xm.biz.BrandBiz;
import com.xm.biz.GoodsTypeBiz;
import com.xm.biz.impl.BrandBizImpl;
import com.xm.biz.impl.GoodsTypeBizImpl;
import com.xm.po.Brand;
import com.xm.po.GoodsType;
import com.xm.util.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Administrator
 * @create 2020-09-22 11:31
 * @desc 商品管理处理器
 **/
@WebServlet("/goods")
public class GoodsServlet extends HttpServlet {
    //初始化数据源
    BrandBiz brandBiz = new BrandBizImpl();
    GoodsTypeBiz goodsTypeBiz = new GoodsTypeBizImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String type = req.getParameter("type");
        //商品品牌管理(查询所有品牌)
        if (type.equals("brandQueryAll")){
            brandQueryAll(req, resp);
        }
        //商品品牌删除
        else if (type.equals("brandDeleteById")){
            brandDeleteById(req, resp);
        }
        //根据编号查询(修改按钮)
        else if (type.equals("brandSelectById")){
            brandSelectById(req, resp);
        }
        //查找所有商品类型
        else if (type.equals("goodsTypeQueryAll")){
            goodsTypeQueryAll(req, resp);
        }
        //根据编号查找（修改按钮）
        else if (type.equals("goodsTypeSelectById")){
            goodsTypeSelectById(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    //商品品牌管理(查询所有品牌)
    public void brandQueryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Brand> all = brandBiz.getAll();
        req.setAttribute("brandList",all);
        req.getRequestDispatcher("admin/brand.jsp").forward(req,resp);
    }
    //商品品牌删除
    public void brandDeleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String brandId = req.getParameter("brandId");
        boolean b = brandBiz.deleteById(Integer.valueOf(brandId));
        if (b==true){
            req.getRequestDispatcher("admin/brand.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher("admin/brand.jsp").forward(req,resp);
        }
    }
    //根据编号查询(修改按钮)
    public void brandSelectById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String brandId = req.getParameter("brandId");
        Brand byId = brandBiz.findById(Integer.valueOf(brandId));
        PrintWriter out = resp.getWriter();
        out.print(JsonUtils.objectToJson(byId));
        out.flush();
        out.close();
    }
    //查找所有商品类型
    public void goodsTypeQueryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<GoodsType> all = goodsTypeBiz.getAll();
        req.setAttribute("GoodsTypes",all);
        req.getRequestDispatcher("admin/goodsType.jsp").forward(req,resp);
    }
    //根据编号查找（修改按钮）
    public void goodsTypeSelectById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String typeId = req.getParameter("typeId");
        GoodsType goodsType = goodsTypeBiz.selectById(Integer.valueOf(typeId));
        PrintWriter out = resp.getWriter();
        out.print(JsonUtils.objectToJson(goodsType));
        out.flush();
        out.close();
    }
}
